package team.stride.tabs.settings;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import team.stride.R;
import team.stride.database.StrideDB;
import team.stride.database.crud.music.MusicItem;
import team.stride.utils.UtilsStride;
import android.app.Dialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class FileBrowser extends ListActivity
{
	private enum DISPLAYMODE
	{
		ABSOLUTE, RELATIVE;
	}

	private final DISPLAYMODE displayMode = DISPLAYMODE.ABSOLUTE;
	private List<String> directoryEntries = new ArrayList<String>();
	private File currentDirectory = new File("/sdcard");

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle)
	{
		super.onCreate(icicle);
		// setContentView() gets called within the next line,
		// so we do not need it here.
		browseToRoot();
	}

	/**
	 * This function browses to the
	 * root-directory of the file-system.
	 */
	private void browseToRoot()
	{
		browseTo(new File("/sdcard"));
	}

	/**
	 * This function browses up one level
	 * according to the field: currentDirectory
	 */
	private void upOneLevel()
	{
		if (this.currentDirectory.getParent() != null)
			this.browseTo(this.currentDirectory.getParentFile());
	}

	private void browseTo(final File aDirectory)
	{
		if (aDirectory == null)
			return;
		if (aDirectory.isDirectory())
		{
			this.currentDirectory = aDirectory;
			fill(aDirectory.listFiles());
		}
		else
		{
			Log.d("log", "IS FILE" + aDirectory.getAbsolutePath());
			// add to music
			final Dialog modifDialog = new Dialog(this.getParent());
			modifDialog.setContentView(R.layout.bd_music_modify);
			modifDialog.setTitle("Enter music");
			modifDialog.setCancelable(true);
			modifDialog.setCanceledOnTouchOutside(true);

			final EditText song = (EditText) modifDialog.findViewById(R.id.musicModifySong);
			song.setText(aDirectory.getName());

			final EditText singer = (EditText) modifDialog.findViewById(R.id.musicModifySinger);
			singer.setText(aDirectory.getName());

			final EditText file = (EditText) modifDialog.findViewById(R.id.musicModifyFile);
			file.setText(aDirectory.getAbsolutePath());

			final EditText playlist = (EditText) modifDialog.findViewById(R.id.musicModifyPlaylist);
			playlist.setText("default");

			final EditText details = (EditText) modifDialog.findViewById(R.id.musicModifyDetails);
			details.setText("");

			Button okBtn = (Button) modifDialog.findViewById(R.id.musicModifyOk);
			okBtn.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					UtilsStride.insertInDB(StrideDB.MATCH_M, new MusicItem(0, song.getText().toString(), singer
							.getText().toString(), playlist.getText().toString(), file.getText().toString(), details
							.getText().toString()));
					modifDialog.dismiss();
				}
			});
			modifDialog.show();

		}
	}

	private void fill(File[] files)
	{
		if (files == null)
			return;
		this.directoryEntries.clear();

		// Add the "." and the ".." == 'Up one level'
		try
		{
			Thread.sleep(10);
		}
		catch (InterruptedException e1)
		{
			e1.printStackTrace();
		}
		this.directoryEntries.add(".");

		if (this.currentDirectory.getParent() != null)
			this.directoryEntries.add("..");

		switch (this.displayMode)
		{
		case ABSOLUTE:
			for (File file : files)
			{
				this.directoryEntries.add(file.getPath());
			}
			break;
		case RELATIVE: // On relative Mode, we have to add the current-path to
			// the beginning
			int currentPathStringLenght = this.currentDirectory.getAbsolutePath().length();
			for (File file : files)
			{
				this.directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLenght));
			}
			break;
		}

		ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, R.layout.file_row, this.directoryEntries);

		this.setListAdapter(directoryList);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		// Log.d("log", "onListItemClick");
		int selectionRowID = (int) id;
		String selectedFileString = this.directoryEntries.get(selectionRowID);
		// Log.d("log", "selectedFileString = " + selectedFileString);
		if (selectedFileString.equals("."))
		{
			// Refresh
			this.browseTo(this.currentDirectory);
		}
		else if (selectedFileString.equals(".."))
		{
			this.upOneLevel();
		}
		else
		{
			// Log.d("log", "else ./..");
			File clickedFile = null;
			switch (this.displayMode)
			{
			case RELATIVE:
				clickedFile = new File(this.currentDirectory.getAbsolutePath()
						+ this.directoryEntries.get(selectionRowID));
				break;
			case ABSOLUTE:
				clickedFile = new File(this.directoryEntries.get(selectionRowID));
				break;
			}

			if (clickedFile != null)
			{
				this.browseTo(clickedFile);
			}
		}
	}
}
